#include<func.h>
typedef struct Client_s
{
    int netfd;   //连接的哪个netfd
    int isAlive; //存活条件
}Clinet_t;

int main(int argc,char*argv[])
{
    //server
    // ./epoll_liaotian2 192.168.213.128 3333
    ARGS_CHECK(argc,3);
    int sockfd = socket(AF_INET,SOCK_STREAM,0);
    //ready bind
    struct sockaddr_in addr;
    addr.sin_family = AF_INET;
    addr.sin_port = htons(atoi(argv[2]));
    addr.sin_addr.s_addr = inet_addr(argv[1]);
    //bind
    int flags = 1;
    int ret = setsockopt(sockfd,SOL_SOCKET,SO_REUSEADDR,&flags,sizeof(flags));
    ret = bind(sockfd,(struct sockaddr*)&addr,sizeof(addr));
    ERROR_CHECK(ret,-1,"bind");
    //listen
    listen(sockfd,100);//half list ready
    //ready epoll and accept
    int epfd = epoll_create(10);//create epfd
    struct epoll_event event; //epoll_event is struct
    event.events = EPOLLIN;
    event.data.fd = sockfd;
    //epoll_ctl
    //int readyset[10]; 这里用户态就绪队列写错le 
    struct epoll_event readyset[10];
    char buf[4096];
    //net放在外面
    int netfd = -1;
    epoll_ctl(epfd,EPOLL_CTL_ADD,sockfd,&event);//add sockfd
    //buf
    while(1)
    {
       int readynum = epoll_wait(epfd,readyset,10,-1);//先不写超时 -1才是永久超时
       for(int i=0;i<readynum;i++)
       {
            if(readyset[i].data.fd == sockfd)
            {
                netfd = accept(sockfd,NULL,NULL);//暂时不获取客户端地址
                //增加监听
                event.events = EPOLLIN;
                event.data.fd = netfd;
                epoll_ctl(epfd,EPOLL_CTL_ADD,netfd,&event); // add netfd
                event.events = EPOLLIN;
                event.data.fd = STDIN_FILENO;
                epoll_ctl(epfd,EPOLL_CTL_ADD,STDIN_FILENO,&event); //add stdin
                epoll_ctl(epfd,EPOLL_CTL_DEL,sockfd,NULL);// 移除对sockfd的监听
            }
            else if(readyset[i].data.fd == STDIN_FILENO)
            {
                //stdin
                bzero(buf,sizeof(buf));
                ssize_t sret = read(STDIN_FILENO,buf,sizeof(buf));
                if(sret == 0)
                {
                    //说明服务端将客户端踢出去了
                    send(netfd,"nishigehaoren\n",15,0);
                    close(netfd);
                    epoll_ctl(epfd,EPOLL_CTL_DEL,netfd,NULL);
                    epoll_ctl(epfd,EPOLL_CTL_DEL,STDIN_FILENO,NULL);
                    event.events = EPOLLIN;
                    event.data.fd = sockfd;
                    epoll_ctl(epfd,EPOLL_CTL_ADD,sockfd,&event);
                    netfd = -1;
                    break;
                }
                send(netfd,buf,sizeof(buf),0);
            }
            else if(readyset[i].data.fd == netfd)
            {
                //如果客户端就绪
                bzero(buf,sizeof(buf));
                ssize_t sret = recv(netfd,buf,sizeof(buf),0);
                if(sret == 0)
                {
                    printf("hehe\n");
                    close(netfd);
                    epoll_ctl(epfd,EPOLL_CTL_DEL,netfd,NULL);
                    epoll_ctl(epfd,EPOLL_CTL_DEL,STDIN_FILENO,NULL);
                    event.events = EPOLLIN;
                    event.data.fd = sockfd;
                    epoll_ctl(epfd,EPOLL_CTL_ADD,sockfd,&event);
                    netfd = -1;
                    break;                    
                }
                printf("buf = %s\n",buf);
            }
       }
    }
    return 0;
}